package com.ctos.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctos.common.Result;
import com.ctos.dto.SetmealDto;
import com.ctos.entity.Setmeal;
import com.ctos.entity.SetmealDish;
import com.ctos.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;


    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result<Page<SetmealDto>> pageQuery(Integer page, Integer pageSize, String name){
        return  setmealService.setmealPage(page, pageSize, name);

    }

    /**
     * 保存套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache" ,allEntries = true)
    public Result<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveSetmealDto(setmealDto);
        return Result.success("success");
    }

    /**
     * 回显数据
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result<SetmealDto> echo(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getSetmealDto(id);
        return Result.success(setmealDto);
    }

    /**
     * 修改菜品
     * @param setmealDto
     * @return
     */
    @PutMapping
    @CacheEvict(value = "setmealCache" ,key = "#setmealDto.id")
    public Result<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateDishAndDishFlavor(setmealDto);
        return Result.success("success");
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping()
    @CacheEvict(value = "setmealCache" ,allEntries = true)
    public Result<String> delete(@RequestParam("ids") Long[] ids){
        setmealService.delete(ids);
        return Result.success("success");
    }

    /**
     * 停售
     * @param ids
     * @return
     */
    @PostMapping("/status/0")
    @CacheEvict(value = "setmealCache" ,allEntries = true)
    public Result<String> downStatus(@RequestParam("ids") Long[] ids){

        return  setmealService.modifyStatus(0, ids);

    }

    /**
     * 起售
     * @param ids
     * @return
     */
    @PostMapping("/status/1")
    @CacheEvict(value = "setmealCache" ,allEntries = true)
    public Result<String> openStatus(@RequestParam("ids") Long[] ids){

        return setmealService.modifyStatus(1, ids);
    }

    /**
     * 获取套餐列表
     * @param id
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache" ,key = "#id + '_' + '1'" )
    public Result<List<Setmeal>> getSetmealList(@RequestParam("categoryId") Long id){
        List<Setmeal> list = setmealService.getSetmealList(id);
        return Result.success(list);
    }

    /**
     * 获取套餐内拥有的菜品
     * @param setmealId
     * @return
     */
    @GetMapping("/dish/{setmealId}")
    @Cacheable(value = "setmealCache" ,key = "#setmealId + '_' + '1'" )
    public Result<List<SetmealDish>> getSetmealDish( @PathVariable Long setmealId){

        return Result.success( setmealService.getSetmealDish(setmealId));
    }


}
